package com.example.fish.service.impl;

import com.example.fish.mapper.BlogMapper;
import com.example.fish.mapper.IllegalMapper;
import com.example.fish.mapper.UserInfoMapper;
import com.example.fish.pojo.Blog;
import com.example.fish.pojo.BlogExample;
import com.example.fish.pojo.Illegal;
import com.example.fish.pojo.UserInfo;
import com.example.fish.service.BlogService;
import com.example.fish.service.ex.*;
import com.example.fish.utils.SensitiveWordFilter;
import com.example.fish.utils.StaticVariable;
import com.example.fish.vo.BlogList;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Resource
    private SensitiveWordFilter sensitiveWordFilter;
    @Resource
    private IllegalMapper illegalMapper;
    @Override
    public List<Blog> findAllBlog() {
        BlogExample blogExample=new BlogExample();
        List<Blog> blogs = blogMapper.selectByExample(blogExample);
        if (blogs.size()==0){
            throw new BlogNotFindException("未查询到blog");
        }
        return blogs;
    }

    /***
     * 分页查询所有
     */
    @Override
    public PageInfo<Blog> getAllBlogByPage(Integer pageNum, Integer pageSize) {
        try {
//            分页查询
            BlogExample blogExample =new BlogExample();
            PageHelper.startPage(pageNum,pageSize);
            List<Blog> blogs = blogMapper.selectByExample(null);
            PageInfo<Blog> pageInfo = new PageInfo<>(blogs);
            return pageInfo;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public Integer insertBlog(Blog blog, List<MultipartFile> file) {
        List<String> fileList = new ArrayList<>();
//        先将图片保存到指定文件夹中，然后封装blog
        for (MultipartFile multipartFile : file) {
//            遍历数组，将每个图片都保存下来
            String fileName = StaticVariable.updateFile(multipartFile, StaticVariable.BLOGIMG_PATH);
            fileList.add(fileName);
//            System.out.println("fileName"+fileName);
        }
//        获取发布的时间，
        LocalDate current = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-d");
        String  current1 = current.format(formatter);
//        初始化blog信息---userid,title,blogtime,content,blogimg,agree,disagree
        blog.setBlogtime(current1);
        blog.setBlogimg(fileList.toString());
//        存储blog
//        敏感字判断
        String s = sensitiveWordFilter.filterSensitiveWord(blog.getContent());
        blog.setContent(s);
//        违规加一
        boolean b = sensitiveWordFilter.containsSensitiveWord(blog.getContent());
        if (b){
            Illegal illegal =new Illegal();
            illegal.setUserid(blog.getUserid());
            illegalMapper.insert(illegal);
        }
        int insert = blogMapper.insert(blog);
        if (insert != 1){
            throw new BlogInsertOrUpdateException("插入失败！");
        }
        return 1 ;
    }

    /***
     * 根据blog id删除
     * @param id blog id
     */

    @Override
    public void deleteBlog(Integer id) {
//        System.out.println("删除"+id);
        int i = blogMapper.deleteByPrimaryKey(id);
        if (i != 1){
            throw new BlogDeleteException("删除失败1");
        }
    }

    /***
     * 根据userid查询
     * @param id userid
     * @return list
     */
    @Override
    public List<Blog> findBlogByUserId(Integer id) {
        BlogExample blogExample = new BlogExample();
        blogExample.createCriteria().andUseridEqualTo(id);
        List<Blog> blogs = blogMapper.selectByExample(blogExample);
        if (blogs.size() == 0){
            throw new BlogNotFindException("未查询到该用户发表的博客");
        }
        return blogs;
    }

    /***
     * 根据id进行分页查询
     * @param id userid
     * @return List
     */
    @Override
    public PageInfo<Blog> findBlogByUserIdPage(Integer pageNum,Integer pageSize,Integer id) {
        try {
            BlogExample blogExample=new BlogExample();
            blogExample.createCriteria().andUseridEqualTo(id);
            PageHelper.startPage(pageNum,pageSize);
            List<Blog> blogs = blogMapper.selectByExample(blogExample);
            PageInfo<Blog> pageInfo = new PageInfo<>(blogs);
            return pageInfo;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /***
     * 喜欢/不喜欢
     * @param blogid 操作的博客id
     */
    @Override
    public void agreeBlog(Integer blogid) {
//        先根据blogid得到blog对象
        Blog blog = blogMapper.selectByPrimaryKey(blogid);
//        操作agree字段+1
        blog.setAgree(blog.getAgree()+1);
        int i = blogMapper.updateByPrimaryKey(blog);
        if (i != 1){
            throw new BlogAgreeOrDisagreeException("点赞成功");
        }

    }

    @Override
    public void agreeDeBlog(Integer blogid) {
        //        先根据blogid得到blog对象
        Blog blog = blogMapper.selectByPrimaryKey(blogid);
//        操作agree字段-1
        blog.setAgree(blog.getAgree()-1);
        if (blog.getAgree() < 0){
            blog.setAgree(0);
        }
        int i = blogMapper.updateByPrimaryKey(blog);
        if (i != 1){
            throw new BlogAgreeOrDisagreeException("取消点赞成功");
        }
    }

    /***
     * disagree的操作
     */
    @Override
    public void disagreeBlog(Integer blogid) {
        //        先根据blogid得到blog对象
        Blog blog = blogMapper.selectByPrimaryKey(blogid);
//        操作disagree字段+1
        blog.setDisagree(blog.getDisagree()+1);
        int i = blogMapper.updateByPrimaryKey(blog);
        if (i != 1){
            throw new BlogAgreeOrDisagreeException("踩一脚");
        }
    }

    @Override
    public void disagreeDeBlog(Integer blogid) {
        //        先根据blogid得到blog对象
        Blog blog = blogMapper.selectByPrimaryKey(blogid);
//        操作disagree字段+1
        blog.setDisagree(blog.getDisagree()-1);
        if (blog.getDisagree() < 0){
            blog.setDisagree(0);
        }
        int i = blogMapper.updateByPrimaryKey(blog);
        if (i != 1){
            throw new BlogAgreeOrDisagreeException("踩一脚");
        }
    }

    /***
     * 根据blogid查询博客
     * @param blogid id
      * @return blog
     */
    @Override
    public Blog findBlogById(Integer blogid) {
        Blog blog = blogMapper.selectByPrimaryKey(blogid);
        if (blog == null){
            throw new BlogNotFindException("未查询到该blog");
        }
        return blog;
    }

    @Override
    public PageInfo<BlogList> homePage(Integer pageNum) {
        Integer pageSize =  5;
//        先分页查询
        BlogExample example = new BlogExample();
        example.setOrderByClause("postid desc");
        int i = blogMapper.countByExample(example);
        PageHelper.startPage(pageNum,pageSize);
        List<Blog> blogs = blogMapper.selectByExample(example);
        List<BlogList> blogLists = new ArrayList<>();
        for (Blog blog : blogs) {
//            遍历blog查询user信息，并封装
            UserInfo userInfo = userInfoMapper.selectById(blog.getUserid());
            BlogList blogList = new BlogList();
            blogList.setBlog(blog);
            blogList.setUserid(userInfo.getUserid());
            blogList.setAvatar(userInfo.getAvator());
            blogList.setUsername(userInfo.getName());
            blogLists.add(blogList);
        }
        PageInfo<BlogList> pageInfo = new PageInfo<>(blogLists);
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageNum(pageNum);
        pageInfo.setTotal(i);
        return pageInfo;
    }


}
